package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;

import android.os.Bundle;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.stark.picselect.config.PhotoConfig;
import com.stark.picselect.entity.SelectMediaEntity;
import com.stark.picselect.utils.MediaLoadManager;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.SelVideoAdapter;
import flc.ast.databinding.ActivitySelLocalVideoBinding;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.utils.RxUtil;
import stark.common.basic.utils.StkPermissionHelper;

public class SelLocalVideoActivity extends BaseAc<ActivitySelLocalVideoBinding> {

    private SelVideoAdapter selVideoAdapter;
    private List<SelectMediaEntity> listShow = new ArrayList<>();

    @Override
    protected int onCreate() {
        return R.layout.activity_sel_local_video;
    }

    @Override
    protected void initView() {
        mDataBinding.ivSelLocalVideoBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        mDataBinding.rvSelVideoList.setLayoutManager(new GridLayoutManager(mContext, 3));
        selVideoAdapter = new SelVideoAdapter();
        mDataBinding.rvSelVideoList.setAdapter(selVideoAdapter);
        selVideoAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        LocalPlayActivity.videoPath=selVideoAdapter.getItem(position).getPath();
        startActivity(LocalPlayActivity.class);
    }

    @Override
    protected void initData() {
        getPermission();
    }

    //获取权限
    private void getPermission() {
        StkPermissionHelper.permission(StkPermissionHelper.Permission.ACCESS_IMAGE_VIDEO).reqPermissionDesc(getString(R.string.get_storage_permission)).callback(new StkPermissionHelper.ACallback() {
            @Override
            public void onGranted() {
                getData();
            }

            @Override
            public void onDenied(boolean needGoSettings) {
                mDataBinding.rvSelVideoList.setVisibility(View.GONE);
                mDataBinding.tvSelVideoNoData.setVisibility(View.VISIBLE);
            }
        }).request();
    }

    //获取视频和图片数据
    private void getData() {
        listShow.clear();
        RxUtil.create(new RxUtil.Callback<List<SelectMediaEntity>>() {
            @Override
            public void doBackground(ObservableEmitter<List<SelectMediaEntity>> emitter) {
                List<SelectMediaEntity> listSys = MediaLoadManager.loadAllMediaData(mContext, PhotoConfig.Mode.VIDEO);
                emitter.onNext(listSys);
            }

            @Override
            public void accept(List<SelectMediaEntity> selectMediaEntities) {
                if (selectMediaEntities.size() > 0) {
                    listShow.addAll(selectMediaEntities);
                    selVideoAdapter.setList(listShow);
                    mDataBinding.rvSelVideoList.setVisibility(View.VISIBLE);
                    mDataBinding.tvSelVideoNoData.setVisibility(View.GONE);
                } else {
                    mDataBinding.rvSelVideoList.setVisibility(View.GONE);
                    mDataBinding.tvSelVideoNoData.setVisibility(View.VISIBLE);
                }
            }
        });
    }

}